home *** CD-ROM | disk | FTP | other *** search
/ Utilities Professional 1-1500 / Utilities Professional 1-1500 (1994)(WPD)[!].iso / 12511500 / var1459.dms / var1459.adf / LowLevelGraphics / Example6.c < prev    next >
C/C++ Source or Header  |  1992-04-28  |  10KB  |  329 lines

  1. /***********************************************************/
  2. /*                                                         */
  3. /* Amiga C Encyclopedia (ACE) V3.0      Amiga C Club (ACC) */
  4. /* -------------------------------      ------------------ */
  5. /*                                                         */
  6. /* Book:    ACM Graphics                Amiga C Club       */
  7. /* Chapter: LowLevelGraphics            Tulevagen 22       */
  8. /* File:    Example6.c                  181 41  LIDINGO    */
  9. /* Author:  Anders Bjerin               SWEDEN             */
  10. /* Date:    92-04-28                                       */
  11. /* Version: 1.00                                           */
  12. /*                                                         */
  13. /*   Copyright 1992, Anders Bjerin - Amiga C Club (ACC)    */
  14. /*                                                         */
  15. /* Registered members may use this program freely in their */
  16. /*     own commercial/noncommercial programs/articles.     */
  17. /*                                                         */
  18. /***********************************************************/
  19.  
  20. /* This example demonstrates how to create a ViewPort in dual playfield  */
  21. /* mode. Playfield 1 use four colours and is placed behind playfield 2   */
  22. /* which only use two colours (transparent and grey). Playfield 1 is     */
  23. /* filled with a lot of dots and is scrolled around while playfield 2 is */
  24. /* is not moved and is filled with only five grey rectangles.            */
  25.  
  26.  
  27. #include <intuition/intuition.h>
  28. #include <graphics/gfxbase.h>
  29.  
  30.  
  31. #define DWIDTH   320 /* Display 320 pixels wide (low resolution).     */
  32. #define DHEIGHT  200 /* Display 200 lines tall (NTSC non interlaced). */
  33.  
  34. #define RWIDTH1  600 /* 600 pixels wide.                              */
  35. #define RHEIGHT1 300 /* 300 lines high.                               */
  36. #define DEPTH1     2 /* Playfield one should use 2 BitPlanes.         */
  37.  
  38. #define RWIDTH2  320 /* 320 pixels wide.                              */
  39. #define RHEIGHT2 200 /* 200 lines high.                               */
  40. #define DEPTH2     1 /* Playfield two should use 1 BitPlane.          */
  41.  
  42. #define COLOURS   10 /* PF1: colours 0-3, PF2: colours 8 and 9. (0-9) */
  43.  
  44. #define SPEED      1 /* How many pixels the Raster should be scrolled */
  45.                      /* every time.                                   */
  46.  
  47. #define BOXES      5 /* Draw 5 rectangles in the second playfield.    */
  48.  
  49.  
  50. struct IntuitionBase *IntuitionBase;
  51. struct GfxBase *GfxBase;
  52.  
  53.  
  54. struct View view;
  55. struct View *old_view;
  56. struct ViewPort view_port;
  57.  
  58. /* Playfield 1: */
  59. struct RasInfo ras_info1;
  60. struct BitMap bit_map1;
  61. struct RastPort rast_port1;
  62.  
  63. /* Playfield 2: */
  64. struct RasInfo ras_info2;
  65. struct BitMap bit_map2;
  66. struct RastPort rast_port2;
  67.  
  68.  
  69. UWORD color_table[] =
  70. {
  71.   0x000, /* Colour 0, Black       */
  72.   0xF00, /* Colour 1, Red         */
  73.   0x0F0, /* Colour 2, Green       */
  74.   0x00F, /* Colour 3, Blue        */
  75.   0x000, /* Colour 4, Not used    */
  76.   0x000, /* Colour 5,   - " -     */
  77.   0x000, /* Colour 6,   - " -     */
  78.   0x000, /* Colour 7,   - " -     */
  79.   0x000, /* Colour 8, Transparent */
  80.   0x888  /* Colour 9, Grey        */
  81. };
  82.  
  83.  
  84. UWORD box[ BOXES ][ 4 ] =
  85. {
  86.   /*  Minimum  Maximum */
  87.   /*  X    Y    X    Y */
  88.   {    0,   0,  50,  20 },
  89.   {  150,  30, 260,  50 },
  90.   {  290, 100, 319, 150 },
  91.   {  150, 170, 210, 199 },
  92.   {   20,  70,  90, 170 }
  93. };
  94.  
  95.  
  96. void clean_up();
  97. void main();
  98.  
  99.  
  100. void main()
  101. {
  102.   SHORT deltaX = SPEED;
  103.   SHORT deltaY = SPEED;
  104.   UWORD *pointer;
  105.   int loop;
  106.   
  107.  
  108.   /* Open the Intuition library: */
  109.   IntuitionBase = (struct IntuitionBase *)
  110.     OpenLibrary( "intuition.library", 0 );
  111.   if( !IntuitionBase )
  112.     clean_up( "Could NOT open the Intuition library!" );
  113.  
  114.   /* Open the Graphics library: */
  115.   GfxBase = (struct GfxBase *)
  116.     OpenLibrary( "graphics.library", 0 );
  117.   if( !GfxBase )
  118.     clean_up( "Could NOT open the Graphics library!" );
  119.  
  120.  
  121.  
  122.   /* Save the current View, so we can restore it later: */
  123.   old_view = GfxBase->ActiView;
  124.  
  125.  
  126.  
  127.   /* 1. Prepare the View structure, and give it a pointer to */
  128.   /*    the first ViewPort:                                  */
  129.   InitView( &view );
  130.   view.ViewPort = &view_port;
  131.  
  132.  
  133.  
  134.   /* 2. Prepare the ViewPort structure, and set some important values: */
  135.   InitVPort( &view_port );
  136.   view_port.DWidth = DWIDTH;        /* Set the width.                  */
  137.   view_port.DHeight = DHEIGHT;      /* Set the height.                 */
  138.   view_port.RasInfo = &ras_info1;   /* Give it a pointer to RasInfo.   */
  139.   view_port.Modes = DUALPF|PFBA;    /* Dual playfields, 2 on top of 1. */
  140.  
  141.  
  142.   /* 3. Get a colour map, link it to the ViewPort, and prepare it: */
  143.   view_port.ColorMap = (struct ColorMap *) GetColorMap( COLOURS );
  144.   if( view_port.ColorMap == NULL )
  145.     clean_up( "Could NOT get a ColorMap!" );
  146.  
  147.   /* Get a pointer to the colour map: */
  148.   pointer = (UWORD *) view_port.ColorMap->ColorTable;
  149.  
  150.   /* Set the colours: */
  151.   for( loop = 0; loop < COLOURS; loop++ )
  152.     *pointer++ = color_table[ loop ];
  153.  
  154.  
  155.  
  156.   /* 4. Prepare the BitMaps: */
  157.  
  158.   /* Playfield 1: */
  159.   InitBitMap( &bit_map1, DEPTH1, RWIDTH1, RHEIGHT1 );
  160.   /* Allocate memory for the Raster: */ 
  161.   for( loop = 0; loop < DEPTH1; loop++ )
  162.   {
  163.     bit_map1.Planes[ loop ] = (PLANEPTR) AllocRaster( RWIDTH1, RHEIGHT1 );
  164.     if( bit_map1.Planes[ loop ] == NULL )
  165.       clean_up( "Could NOT allocate enough memory for the raster!" );
  166.  
  167.     /* Clear the display memory with help of the Blitter: */
  168.     BltClear( bit_map1.Planes[ loop ], RASSIZE( RWIDTH1, RHEIGHT1 ), 0 );
  169.   }
  170.  
  171.   /* Playfield 2: */
  172.   InitBitMap( &bit_map2, DEPTH2, RWIDTH2, RHEIGHT2 );
  173.   /* Allocate memory for the Raster: */ 
  174.   for( loop = 0; loop < DEPTH2; loop++ )
  175.   {
  176.     bit_map2.Planes[ loop ] = (PLANEPTR) AllocRaster( RWIDTH2, RHEIGHT2 );
  177.     if( bit_map2.Planes[ loop ] == NULL )
  178.       clean_up( "Could NOT allocate enough memory for the raster!" );
  179.  
  180.     /* Clear the display memory with help of the Blitter: */
  181.     BltClear( bit_map2.Planes[ loop ], RASSIZE( RWIDTH2, RHEIGHT2 ), 0 );
  182.   }
  183.  
  184.  
  185.  
  186.   /* 5. Prepare the RasInfo structures: */
  187.  
  188.   /* Playfield 1: */
  189.   ras_info1.BitMap = &bit_map1; /* Pointer to the BitMap structure.  */
  190.   ras_info1.RxOffset = 0;       /* The top left corner of the Raster */
  191.   ras_info1.RyOffset = 0;       /* should be at the top left corner  */
  192.                                 /* of the display.                   */
  193.   ras_info1.Next = &ras_info2;  /* Link RasInfo1 to RasInfo2.        */
  194.  
  195.   /* Playfield 2: */
  196.   ras_info2.BitMap = &bit_map2; /* Pointer to the BitMap structure.  */
  197.   ras_info2.RxOffset = 0;       /* The top left corner of the Raster */
  198.   ras_info2.RyOffset = 0;       /* should be at the top left corner  */
  199.                                 /* of the display.                   */
  200.   ras_info2.Next = NULL;        /* Last RasInfo structure.           */
  201.  
  202.  
  203.  
  204.   /* 6. Create the display: */
  205.   MakeVPort( &view, &view_port );
  206.   MrgCop( &view );
  207.  
  208.  
  209.  
  210.   /* 7. Prepare the RastPorts, and give them a pointer to each BitMap. */
  211.  
  212.   /* Playfield 1: */
  213.   InitRastPort( &rast_port1 );
  214.   rast_port1.BitMap = &bit_map1;
  215.  
  216.   /* Playfield 2: */
  217.   InitRastPort( &rast_port2 );
  218.   rast_port2.BitMap = &bit_map2;
  219.   
  220.  
  221.  
  222.   /* 8. Show the new View: */
  223.   LoadView( &view );
  224.  
  225.  
  226.  
  227.   /* Playfield 2: */
  228.   /* Set the draw mode to JAM1. FgPen's colour will be used. */
  229.   SetDrMd( &rast_port2, JAM1 );
  230.   /* Use colour 9 (grey): */
  231.   SetAPen( &rast_port2, 9 );
  232.   /* Draw five grey boxes: */
  233.   for( loop = 0; loop < BOXES; loop++ )
  234.     RectFill( &rast_port2, box[ loop ][ 0 ], 
  235.                            box[ loop ][ 1 ],
  236.                            box[ loop ][ 2 ],
  237.                            box[ loop ][ 3 ] );
  238.  
  239.  
  240.   /* Playfield 1: */
  241.   /* Set the draw mode to JAM1. FgPen's colour will be used. */
  242.   SetDrMd( &rast_port1, JAM1 );
  243.   /* PF1: Draw 5000 pixels in four different colours, randomly. */ 
  244.   for( loop = 0; loop < 5000; loop++ )
  245.   {
  246.     /* Set FgPen's colour (0-3): */
  247.     SetAPen( &rast_port1, rand() % 4 );
  248.     /* Write a pixel somewere on the display: */
  249.     WritePixel( &rast_port1, rand() % RWIDTH1, rand() % RHEIGHT1 );
  250.   }
  251.  
  252.   /* Scroll the Raster (PF 1) in all directions for a little while: */
  253.   for( loop = 0; loop < 5000; loop++ )
  254.   {
  255.     ras_info1.RxOffset += deltaX;
  256.     ras_info1.RyOffset += deltaY;
  257.  
  258.     /* The Raster is moved in one direction until the other side is */
  259.     /* reached were we change the direction:                        */ 
  260.  
  261.     /* Have we reached the left side? */
  262.     if( ras_info1.RxOffset <= 0 )
  263.       deltaX = SPEED;
  264.     /* Have we reached the right (Raster width - Display width) side? */
  265.     if( ras_info1.RxOffset >= RWIDTH1 - DWIDTH )
  266.       deltaX = -SPEED;
  267.  
  268.     /* Have we reached the top side? */
  269.     if( ras_info1.RyOffset <= 0 )
  270.       deltaY = SPEED;
  271.     /* Have we reached the bottom (Raster height - Display height) side? */
  272.     if( ras_info1.RyOffset >= RHEIGHT1 - DHEIGHT )
  273.       deltaY = -SPEED;
  274.  
  275.  
  276.     /* Recalculate the display instructions: (If you change any values */
  277.     /* in the display structures the Amiga have to recalculate the     */
  278.     /* entire display instructions. You must therefore call all three  */
  279.     /* display functions: MakeVPort(), MrgCop() and LoadView().)       */
  280.     MakeVPort( &view, &view_port );
  281.     MrgCop( &view );
  282.     LoadView( &view );
  283.   }
  284.  
  285.  
  286.  
  287.   /* 9. Restore the old View: */
  288.   LoadView( old_view );
  289.  
  290.  
  291.   /* Free all allocated resources and leave. */
  292.   clean_up( "THE END" );
  293. }
  294.  
  295.  
  296. /* Returns all allocated resources: */
  297. void clean_up( message )
  298. STRPTR message;
  299. {
  300.   int loop;
  301.  
  302.   /* Free automatically allocated display structures: */
  303.   FreeVPortCopLists( &view_port );
  304.   FreeCprList( view.LOFCprList );
  305.   
  306.   /* Deallocate the display memory, BitPlane for BitPlane: */
  307.   /* Playfield 1: */
  308.   for( loop = 0; loop < DEPTH1; loop++ )
  309.     if( bit_map1.Planes[ loop ] )
  310.       FreeRaster( bit_map1.Planes[ loop ], RWIDTH1, RHEIGHT1 );
  311.   /* Playfield 2: */
  312.   for( loop = 0; loop < DEPTH2; loop++ )
  313.     if( bit_map2.Planes[ loop ] )
  314.       FreeRaster( bit_map2.Planes[ loop ], RWIDTH2, RHEIGHT2 );
  315.  
  316.   /* Deallocate the ColorMap: */
  317.   if( view_port.ColorMap ) FreeColorMap( view_port.ColorMap );
  318.  
  319.   /* Close the Graphics library: */
  320.   if( GfxBase ) CloseLibrary( GfxBase );
  321.  
  322.   /* Close the Intuition library: */
  323.   if( IntuitionBase ) CloseLibrary( IntuitionBase );
  324.  
  325.   /* Print the message and leave: */
  326.   printf( "%s\n", message ); 
  327.   exit();
  328. }
  329.